local hx__yuanlv = fk.CreateSkill{
  name = "hx__yuanlv",
  tags = { Skill.Wake },
}

hx__yuanlv:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  if player.maxHp >= #player:getCardIds(Player.Hand) then
    room:notifySkillInvoked(player, hx__yuanlv.name)
    player:broadcastSkillInvoke(hx__yuanlv.name)
    local tos = table.filter(player.room.alive_players, function(p) return p.kingdom ~= "wu" end)
    for _, p in ipairs(tos) do
      local skills = Fk.generals[p.general]:getSkillNameList(true)
      if p.deputyGeneral ~= "" then
        table.insertTableIfNeed(skills, Fk.generals[p.deputyGeneral]:getSkillNameList(true))
      end
      if #skills > 0 then
        if p.deputyGeneral ~= "" then
          table.insertTableIfNeed(skills, Fk.generals[p.deputyGeneral]:getSkillNameList(true))
        end
        local n = math.min(player.maxHp, #skills)
        skills = table.slice(skills, 1, n + 1)
        room:handleAddLoseSkills(p, "-"..table.concat(skills, "|-"))
        room:changeMaxHp(p, n)
        if not p.dead then
          room:changeHp(p, n)
        end
      end
    end
    player:setSkillUseHistory(hx__yuanlv.name, 1, Player.HistoryGame)
  end
end)

hx__yuanlv:addEffect(fk.MaxHpChanged, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__yuanlv.name) and
    player:usedSkillTimes(hx__yuanlv.name, Player.HistoryGame) == 0 then
      return true
    end
  end,
  can_wake = function(self, event, target, player, data)
    local tos = table.filter(player.room.alive_players, function(p) return p.kingdom ~= "wu" end)
    for _, p in ipairs(tos) do
      local skills = Fk.generals[p.general]:getSkillNameList(true)
      if p.deputyGeneral ~= "" then
        table.insertTableIfNeed(skills, Fk.generals[p.deputyGeneral]:getSkillNameList(true))
      end
      if #skills > 0 then
        return player.maxHp >= #player:getCardIds(Player.Hand)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = table.filter(player.room.alive_players, function(p) return p.kingdom ~= "wu" end)
    for _, p in ipairs(tos) do
      local skills = Fk.generals[p.general]:getSkillNameList(true)
      if p.deputyGeneral ~= "" then
        table.insertTableIfNeed(skills, Fk.generals[p.deputyGeneral]:getSkillNameList(true))
      end
      local n = math.min(player.maxHp, #skills)
      skills = table.slice(skills, 1, n + 1)
      room:handleAddLoseSkills(p, "-"..table.concat(skills, "|-"))
      room:changeMaxHp(p, n)
      if not p.dead then
        room:changeHp(p, n)
      end
    end
  end,
})

hx__yuanlv:addEffect(fk.AfterCardsMove, {
  mute = true,
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__yuanlv.name) and
    player:usedSkillTimes(hx__yuanlv.name, Player.HistoryGame) == 0 then
      return true
    end
  end,
  can_wake = function(self, event, target, player, data)
    local tos = table.filter(player.room.alive_players, function(p) return p.kingdom ~= "wu" end)
    for _, p in ipairs(tos) do
      local skills = Fk.generals[p.general]:getSkillNameList(true)
      if p.deputyGeneral ~= "" then
        table.insertTableIfNeed(skills, Fk.generals[p.deputyGeneral]:getSkillNameList(true))
      end
      if #skills > 0 then
        return player.maxHp >= #player:getCardIds(Player.Hand)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, hx__yuanlv.name)
    player:broadcastSkillInvoke(hx__yuanlv.name)
    local tos = table.filter(player.room.alive_players, function(p) return p.kingdom ~= "wu" end)
    for _, p in ipairs(tos) do
      local skills = Fk.generals[p.general]:getSkillNameList(true)
      if p.deputyGeneral ~= "" then
        table.insertTableIfNeed(skills, Fk.generals[p.deputyGeneral]:getSkillNameList(true))
      end
      local n = math.min(player.maxHp, #skills)
      skills = table.slice(skills, 1, n + 1)
      room:handleAddLoseSkills(p, "-"..table.concat(skills, "|-"))
      if n > 0 then
        room:changeMaxHp(p, n)
        if not p.dead then
          room:changeHp(p, n)
        end
      end
    end
  end,
})

return hx__yuanlv
